# coding:utf-8
from django.core.paginator import PageNotAnInteger, EmptyPage
from django.shortcuts import render
from blog.lib.expaginator import ExPaginator
from blog.models import Article, Column

__author__ = 'lenovo'
def index(request):
    articles = Article.objects.all()
    paginator = ExPaginator(articles,2)
    pageNumber = request.GET.get('pageNumber')
    try:
        page = paginator.page(pageNumber)
    except PageNotAnInteger:
        page = paginator.page(1)
    except EmptyPage:
        page = paginator.page(paginator.num_pages)

    # 获得栏目
    columns = Column.objects.all()

    # 最新文章
    lastNum = Article.objects.count()
    if lastNum - 15 < 0 :
        startNum = 0
    else:
        startNum = lastNum - 15
    lastArticles = Article.objects.all()[startNum:lastNum]
    return render(request,'blog/Index/index.html',{'page':page,'columns':columns,'lastArticles':lastArticles})